package org.jeecg.modules.basic.warehouse.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.BeanUtil;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouse;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouseArea;
import org.jeecg.modules.basic.warehouse.entity.vo.BasicWarehouseVO;
import org.jeecg.modules.basic.warehouse.entity.vo.WmsBasicWarehousePageVO;
import org.jeecg.modules.basic.warehouse.entity.vo.WmsBasicWarehouseQueryVO;
import org.jeecg.modules.basic.warehouse.entity.vo.WmsBasicWarehouseVO;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseAreaService;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseService;
import org.jeecg.modules.business.job.service.SyncService;
import org.jeecg.modules.business.repertory.entity.WmsRepertory;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 仓库
 * @Author: jeecg-boot
 * @Date: 2023-08-09
 * @Version: V1.0
 */
@Api(tags = "仓库")
@RestController
@RequestMapping("/basic.warehouse/wmsBasicWarehouse")
@Slf4j
public class WmsBasicWarehouseController extends JeecgController<WmsBasicWarehouse, IWmsBasicWarehouseService> {
    @Resource
    private IWmsBasicWarehouseService wmsBasicWarehouseService;

    @Autowired
    private IWmsRepertoryService repertoryService;

    @Autowired
    private IWmsBasicWarehouseAreaService warehouseAreaService;

    @Autowired
    private SyncService syncService;

    /**
     * 分页列表查询
     *
     * @param wmsBasicWarehouse
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "仓库-分页列表查询")
    @ApiOperation(value = "仓库-分页列表查询", notes = "仓库-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<WmsBasicWarehouse>> queryPageList(WmsBasicWarehouse wmsBasicWarehouse,
                                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                          HttpServletRequest req) {
        QueryWrapper<WmsBasicWarehouse> queryWrapper = QueryGenerator.initQueryWrapper(wmsBasicWarehouse, req.getParameterMap());
        Page<WmsBasicWarehouse> page = new Page<WmsBasicWarehouse>(pageNo, pageSize);
        IPage<WmsBasicWarehouse> pageList = wmsBasicWarehouseService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "仓库-分页列表查询-2", notes = "仓库-分页列表查询-2")
    @GetMapping(value = "/getPageList")
    public Result<IPage<WmsBasicWarehousePageVO>> getPageList(WmsBasicWarehouseQueryVO queryVO,
                                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        IPage<WmsBasicWarehouse> page=new Page<>(pageNo,pageSize);
        IPage<WmsBasicWarehousePageVO> pageList=wmsBasicWarehouseService.getPageList(page,queryVO);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param wmsBasicWarehouse
     * @return
     */
    @AutoLog(value = "仓库-添加")
    @ApiOperation(value = "仓库-添加", notes = "仓库-添加")
    @RequiresPermissions("basic.warehouse:wms_basic_warehouse:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody WmsBasicWarehouse wmsBasicWarehouse) {
        wmsBasicWarehouse.setIsUse("0");
        wmsBasicWarehouseService.save(wmsBasicWarehouse);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param wmsBasicWarehouse
     * @return
     */
    @AutoLog(value = "仓库-编辑")
    @ApiOperation(value = "仓库-编辑", notes = "仓库-编辑")
    @RequiresPermissions("basic.warehouse:wms_basic_warehouse:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody WmsBasicWarehouse wmsBasicWarehouse) {
        wmsBasicWarehouseService.updateByCheck(wmsBasicWarehouse);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "仓库-通过id删除")
    @ApiOperation(value = "仓库-通过id删除", notes = "仓库-通过id删除")
    @RequiresPermissions("basic.warehouse:wms_basic_warehouse:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        long areas = warehouseAreaService.count(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
                .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, id));
        if (areas > 0) {
            throw new JeecgBootException("该仓库有库区数据，不能删除！");
        }
        long count = repertoryService.count(new LambdaQueryWrapper<WmsRepertory>()
                .eq(WmsRepertory::getWmsBasicWarehouseId, id)
                .ge(WmsRepertory::getStockQuantity, 0));
        if (count > 0) {
            throw new JeecgBootException("该仓库有即时库存数据，不能删除！");
        }
        wmsBasicWarehouseService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "仓库-批量删除")
    @ApiOperation(value = "仓库-批量删除", notes = "仓库-批量删除")
    @RequiresPermissions("basic.warehouse:wms_basic_warehouse:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.wmsBasicWarehouseService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "仓库-通过id查询")
    @ApiOperation(value = "仓库-通过id查询", notes = "仓库-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WmsBasicWarehouse> queryById(@RequestParam(name = "id", required = true) String id) {
        WmsBasicWarehouse wmsBasicWarehouse = wmsBasicWarehouseService.getById(id);
        if (wmsBasicWarehouse == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wmsBasicWarehouse);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param wmsBasicWarehouse
     */
    @ApiOperation(value = "导出excel", notes = "导出excel")
    @RequiresPermissions("basic.warehouse:wms_basic_warehouse:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WmsBasicWarehouse wmsBasicWarehouse) {
        return super.exportXls(request, wmsBasicWarehouse, WmsBasicWarehouse.class, "仓库");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value = "通过excel导入数据", notes = "通过excel导入数据")
    @RequiresPermissions("basic.warehouse:wms_basic_warehouse:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WmsBasicWarehouse.class);
    }

    /**
     * 仓库-仓库分类树形结构
     *
     * @return org.jeecg.common.api.vo.Result<java.util.List < org.jeecg.modules.basic.warehouse.entity.vo.BasicWarehouseVO>>
     * @author hansen
     * @since 2023/8/11 8:34
     */
    @AutoLog(value = "仓库-仓库分类树形结构")
    @ApiOperation(value = "仓库-仓库分类树形结构", notes = "仓库-仓库分类树形结构")
    @GetMapping(value = "/getWarehouseList")
    public Result<List<BasicWarehouseVO>> getWarehouseList() {
        return Result.ok(wmsBasicWarehouseService.listTree());
    }


    /**
     * 仓库-同步sap的仓库
     *
     * @author zhaojun
     * @since 2023/11/13 8:34
     */
    @AutoLog(value = "仓库-同步sap的仓库")
    @ApiOperation(value = "仓库-同步sap的仓库", notes = "仓库-同步sap的仓库")
    @GetMapping(value = "/sync-sap-location")
    public Result syncWarehouse() {
        syncService.syncWarehouse();
        return Result.ok();
    }
    /**
     * 仓库下拉框
     *
     * @author hansen
     * @since 2023/11/13 16:49
     * @return org.jeecg.common.api.vo.Result<java.util.List<org.jeecg.modules.basic.warehouse.entity.vo.WmsBasicWarehouseVO>>
     */
    @AutoLog(value = "仓库下拉框")
    @ApiOperation(value = "仓库下拉框", notes = "仓库下拉框")
    @GetMapping(value = "/warehouse-list")
    public Result<List<WmsBasicWarehouseVO>> warehouseList() {
        return Result.ok(BeanUtil.copyToList(wmsBasicWarehouseService.list(new LambdaQueryWrapper<WmsBasicWarehouse>()), WmsBasicWarehouseVO.class));
    }
}
